<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of mmw_command_DeleteBookmarkCommand
 *
 * @author Manu
 */
require_once("mmw_command_Command.php");
require_once(dirname(__FILE__) . "/../core/mmw_core_AppException.php");
require_once(dirname(__FILE__) . "/../controller/mmw_controller_Request.php");
require_once(dirname(__FILE__) . "/../mapper/mmw_mapper_WebpageMapper.php");
require_once(dirname(__FILE__) . "/../mapper/mmw_mapper_TagMapper.php");
require_once(dirname(__FILE__) . "/../mapper/mmw_mapper_BookmarkMapper.php");

class mmw_command_DeleteBookmarkCommand extends mmw_command_Command {

    public function doExecute(mmw_controller_Request $request) {
        $success = true;
        $missingParameter;

        if ($request->getProperty("url") == null) {
            $success = false;
            $missingParameter = "url";
        } else if ($request->getProperty("tag") == null) {
            $success = false;
            $missingParameter = "tag";
        }

        if (!$success) {
            throw new mmw_core_AppException("Missing parameter '$missingParameter'");
        }

        $strUrl = $request->getProperty("url");
        $strTag = $request->getProperty("tag");

        //1) we fetch the ids of the webpage and tag identified by the url and tag parameters
        //a) first the webpage
        $webpageMapper = new mmw_mapper_WebpageMapper();
        $webpage = $webpageMapper->findByUrl($strUrl);

        if ($webpage == null) {
            throw new mmw_core_AppException("Unknown url '$strUrl'");
        }

        //b) then the tag
        $tagMapper = new mmw_mapper_TagMapper();
        $tag = $tagMapper->findByTag($strTag);

        if ($tag == null) {
            throw new mmw_core_AppException("Uknown tag '$strTag'");
        }

        //2) Is this webpage_id associated with this tags_id ?
        $bookmarkMapper = new mmw_mapper_BookmarkMapper();
        if (!$bookmarkMapper->existsHasIds($webpage->getId(), $tag->getId())) {
            throw new mmw_core_AppException("No association found for (webpage_id,tags_id) : (" . $webpage->getId() . "," . $tag->getId() . ")");
        }

        //yes they are associated. we remove the association        
        $bookmarkMapper->deleteIds($webpage->getId(), $tag->getId());

        $request->addFeedback("Removed association (webpage_id,tags_id) : (" . $webpage->getId() . "," . $tag->getId() . ")");

        //3) now we need to clean up the db. Is this webpage still associated with some other tag ?
        if (!$bookmarkMapper->existsHasWebpageId($webpage->getId())) {
            //no it's not: we remove the webpage
            $webpageMapper->delete($webpage->getId());
            $request->addFeedback("Removed webpage with id=" . $webpage->getId());
        }

        //Is thjs tag still associated with some other webpage ?
        if (!$bookmarkMapper->existsHasTagId($tag->getId())) {
            //no it's not: we remove the tag
            $tagMapper->delete($tag->getId());
            $request->addFeedback("Removed tag with id=" . $tag->getId());
        }
    }

}

//********TESTS
//$deleteBookmarkCmd=new mmw_command_DeleteBookmarkCommand();
//$request=new mmw_controller_Request();
//$request->setProperty("url","whatever138.com");
//$request->setProperty("tag","test169");
//$deleteBookmarkCmd->doExecute($request);
//
//var_dump($request->getFeedback());
//useful URL for creating
//http://localhost/markMyWeb2/index.php?target=bookmark&action=create&url=whatever126.com&title=whatever9&tags=test165
?>
